package com.lineage.server.model.Instance;

import com.lineage.server.IdFactory;
import com.lineage.server.IdFactoryNpc;
import com.lineage.server.datatables.ExpTable;
import com.lineage.server.datatables.NpcTable;
import com.lineage.server.datatables.PetItemTable;
import com.lineage.server.datatables.PetTypeTable;
import com.lineage.server.datatables.lock.PetReading;
import com.lineage.server.model.L1AttackPc;
import com.lineage.server.model.L1Character;
import com.lineage.server.model.L1GroundInventory;
import com.lineage.server.model.L1Inventory;
import com.lineage.server.model.L1Location;
import com.lineage.server.model.L1Object;
import com.lineage.server.model.L1PcInventory;
import com.lineage.server.serverpackets.S_DoActionGFX;
import com.lineage.server.serverpackets.S_HPMeter;
import com.lineage.server.serverpackets.S_NPCPack_Pet;
import com.lineage.server.serverpackets.S_NpcChat;
import com.lineage.server.serverpackets.S_PetMenuPacket;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.S_SkillHaste;
import com.lineage.server.serverpackets.S_SkillSound;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.templates.L1Npc;
import com.lineage.server.templates.L1Pet;
import com.lineage.server.templates.L1PetItem;
import com.lineage.server.templates.L1PetType;
import com.lineage.server.types.Point;
import com.lineage.server.world.World;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class L1PetInstance extends L1NpcInstance {
  private static final long serialVersionUID = 1L;
  
  private static final Log _log = LogFactory.getLog(L1PetInstance.class);
  
  private static Random _random = new Random();
  
  private int _currentPetStatus;
  
  private int _checkMove;
  
  private L1PcInstance _petMaster;
  
  private int _itemObjId;
  
  private L1PetType _type;
  
  private int _expPercent;
  
  private L1ItemInstance _weapon;
  
  private L1ItemInstance _armor;
  
  private int _hitByWeapon;
  
  private int _damageByWeapon;
  
  private int _tempModel;
  
  public boolean noTarget() {
    switch (this._currentPetStatus) {
      case 3:
        return true;
      case 4:
        if (this._petMaster == null || this._petMaster.getMapId() != getMapId() || 
          getLocation().getTileLineDistance((Point)this._petMaster.getLocation()) >= 5) {
          this._currentPetStatus = 3;
          return true;
        } 
        if (this._npcMove != null) {
          int dir = this._npcMove.targetReverseDirection(this._petMaster.getX(), this._petMaster.getY());
          dir = this._npcMove.checkObject(dir);
          this._npcMove.setDirectionMove(dir);
          setSleepTime(calcSleepTime(getPassispeed(), 0));
        } 
        return false;
      case 5:
        if ((Math.abs(getHomeX() - getX()) > 1 || Math.abs(getHomeY() - getY()) > 1) && this._npcMove != null) {
          int dir = this._npcMove.moveDirection(getHomeX(), getHomeY());
          if (dir == -1) {
            setHomeX(getX());
            setHomeY(getY());
          } else {
            this._npcMove.setDirectionMove(dir);
            setSleepTime(calcSleepTime(getPassispeed(), 0));
          } 
        } 
        return false;
      case 7:
        if (this._petMaster != null && this._petMaster.getMapId() == getMapId() && getLocation().getTileLineDistance((Point)this._petMaster.getLocation()) <= 1) {
          this._currentPetStatus = 3;
          return true;
        } 
        if (this._npcMove != null) {
          int locx = this._petMaster.getX() + _random.nextInt(1);
          int locy = this._petMaster.getY() + _random.nextInt(1);
          int dirx = this._npcMove.moveDirection(locx, locy);
          if (dirx == -1) {
            this._currentPetStatus = 3;
            return true;
          } 
          this._npcMove.setDirectionMove(dirx);
          setSleepTime(calcSleepTime(getPassispeed(), 0));
        } 
        return false;
    } 
    if (this._petMaster == null || this._petMaster.getMapId() != getMapId()) {
      this._currentPetStatus = 3;
      return true;
    } 
    if (getLocation().getTileLineDistance((Point)this._petMaster.getLocation()) > 2 && this._npcMove != null) {
      int dir = this._npcMove.moveDirection(this._petMaster.getX(), this._petMaster.getY());
      if (dir != -1) {
        this._checkMove = 0;
        this._npcMove.setDirectionMove(dir);
        setSleepTime(calcSleepTime(getPassispeed(), 0));
      } else {
        this._checkMove++;
        if (this._checkMove >= 10) {
          this._checkMove = 0;
          this._currentPetStatus = 3;
          return true;
        } 
      } 
    } 
    return false;
  }
  
  public L1PetInstance(L1Npc template, L1PcInstance master) {
    super(template);
    this._checkMove = 0;
    this._tempModel = 3;
    this._petMaster = master;
    this._itemObjId = -1;
    this._type = null;
    setId(IdFactoryNpc.get().nextId());
    set_showId(master.get_showId());
    setName(template.get_name());
    setLevel(template.get_level());
    setMaxHp(template.get_hp());
    setCurrentHpDirect(template.get_hp());
    setMaxMp(template.get_mp());
    setCurrentMpDirect(template.get_mp());
    setExp(template.get_exp());
    setExpPercent(ExpTable.getExpPercentage(template.get_level(), template.get_exp()));
    setLawful(template.get_lawful());
    setTempLawful(template.get_lawful());
    setMaster(master);
    setX(master.getX() + _random.nextInt(5) - 2);
    setY(master.getY() + _random.nextInt(5) - 2);
    setMap(master.getMapId());
    setHeading(master.getHeading());
    setLightSize(template.getLightSize());
    this._currentPetStatus = 3;
    World.get().storeObject((L1Object)this);
    World.get().addVisibleObject((L1Object)this);
    Iterator<L1PcInstance> iterator = World.get().getRecognizePlayer((L1Object)this).iterator();
    while (iterator.hasNext()) {
      L1PcInstance pc = iterator.next();
      onPerceive(pc);
    } 
    master.addPet(this);
    addMaster(master);
  }
  
  public L1PetInstance(L1Npc template, L1PcInstance master, L1Pet pet) {
    super(template);
    this._checkMove = 0;
    this._tempModel = 3;
    this._petMaster = master;
    this._itemObjId = pet.get_itemobjid();
    this._type = PetTypeTable.getInstance().get(template.get_npcId());
    setId(pet.get_objid());
    set_showId(master.get_showId());
    setName(pet.get_name());
    setLevel(pet.get_level());
    setMaxHp(pet.get_hp());
    setCurrentHpDirect(pet.get_hp());
    setMaxMp(pet.get_mp());
    setCurrentMpDirect(pet.get_mp());
    setExp(pet.get_exp());
    setExpPercent(ExpTable.getExpPercentage(pet.get_level(), pet.get_exp()));
    setLawful(pet.get_lawful());
    setTempLawful(pet.get_lawful());
    setMaster(master);
    setX(master.getX() + _random.nextInt(5) - 2);
    setY(master.getY() + _random.nextInt(5) - 2);
    setMap(master.getMapId());
    setHeading(5);
    setLightSize(template.getLightSize());
    this._currentPetStatus = 1;
    World.get().storeObject((L1Object)this);
    World.get().addVisibleObject((L1Object)this);
    Iterator<L1PcInstance> iterator = World.get().getRecognizePlayer((L1Object)this).iterator();
    while (iterator.hasNext()) {
      L1PcInstance pc = iterator.next();
      onPerceive(pc);
    } 
    broadcastPacketAll((ServerBasePacket)new S_SkillSound(getId(), 3104));
    broadcastPacketAll((ServerBasePacket)new S_SkillHaste(getId(), 1, 0));
    setMoveSpeed(1);
    setSkillEffect(43, 1800000);
    master.addPet(this);
    addMaster(master);
  }
  
  public L1PetInstance(L1NpcInstance target, L1PcInstance master, int itemobjid) {
    super((L1Npc)null);
    this._checkMove = 0;
    this._tempModel = 3;
    this._petMaster = master;
    this._itemObjId = itemobjid;
    this._type = PetTypeTable.getInstance().get(target.getNpcTemplate().get_npcId());
    setId(IdFactory.get().nextId());
    set_showId(master.get_showId());
    setting_template(target.getNpcTemplate());
    setCurrentHpDirect(target.getCurrentHp());
    setCurrentMpDirect(target.getCurrentMp());
    setExp(750L);
    setExpPercent(0);
    setLawful(0);
    setTempLawful(0);
    setMaster(master);
    setX(target.getX());
    setY(target.getY());
    setMap(target.getMapId());
    setHeading(target.getHeading());
    setLightSize(target.getLightSize());
    setPetcost(6);
    setInventory(target.getInventory());
    target.setInventory((L1Inventory)null);
    this._currentPetStatus = 3;
    target.deleteMe();
    World.get().storeObject((L1Object)this);
    World.get().addVisibleObject((L1Object)this);
    Iterator<L1PcInstance> iterator = World.get().getRecognizePlayer((L1Object)this).iterator();
    while (iterator.hasNext()) {
      L1PcInstance pc = iterator.next();
      onPerceive(pc);
    } 
    master.addPet(this);
    PetReading.get().storeNewPet(target, getId(), itemobjid);
    addMaster(master);
  }
  
  public L1PetInstance(int npcid, L1PcInstance master, int itemobjid) {
    super((L1Npc)null);
    this._checkMove = 0;
    this._tempModel = 3;
    this._petMaster = master;
    this._itemObjId = itemobjid;
    this._type = PetTypeTable.getInstance().get(npcid);
    L1Npc npc = NpcTable.get().getTemplate(npcid);
    setId(IdFactory.get().nextId());
    set_showId(master.get_showId());
    setting_template(npc);
    setCurrentHpDirect(npc.get_hp());
    setCurrentMpDirect(npc.get_mp());
    long exp = 750L;
    setExp(750L);
    setLevel(ExpTable.getLevelByExp(750L));
    setExpPercent(0);
    setLawful(0);
    setTempLawful(0);
    setMaster(master);
    L1Location loc = master.getLocation().randomLocation(5, false);
    setX(loc.getX());
    setY(loc.getY());
    setMap((short)loc.getMapId());
    setHeading(5);
    setLightSize(npc.getLightSize());
    setPetcost(6);
    this._currentPetStatus = 3;
    World.get().storeObject((L1Object)this);
    World.get().addVisibleObject((L1Object)this);
    Iterator<L1PcInstance> iterator = World.get().getRecognizePlayer((L1Object)this).iterator();
    while (iterator.hasNext()) {
      L1PcInstance pc = iterator.next();
      onPerceive(pc);
    } 
    master.addPet(this);
    PetReading.get().storeNewPet(this, getId(), itemobjid);
    addMaster(master);
  }
  
  public void receiveDamage(L1Character attacker, int damage) {
    this.ISASCAPE = false;
    if (getCurrentHp() > 0) {
      if (damage > 0) {
        setHate(attacker, 0);
        removeSkillEffect(66);
        removeSkillEffect(212);
      } 
      if (attacker instanceof L1PcInstance && damage > 0) {
        L1PcInstance player = (L1PcInstance)attacker;
        player.setPetTarget(this);
      } 
      int newHp = getCurrentHp() - damage;
      if (newHp <= 0) {
        death(attacker);
      } else {
        setCurrentHp(newHp);
      } 
    } else if (!isDead()) {
      death(attacker);
    } 
  }
  
  public synchronized void death(L1Character lastAttacker) {
    if (!isDead()) {
      setDead(true);
      setStatus(8);
      setCurrentHp(0);
      setExpPercent(getExpPercent() - 5);
      this._currentPetStatus = 3;
      getMap().setPassable((Point)getLocation(), true);
      broadcastPacketAll((ServerBasePacket)new S_DoActionGFX(getId(), 8));
    } 
  }
  
  public void evolvePet(int new_itemobjid) {
    L1Pet pet = PetReading.get().getTemplate(this._itemObjId);
    if (pet == null)
      return; 
    int newNpcId = this._type.getNpcIdForEvolving();
    int evolvItem = this._type.getEvolvItemId();
    int tmpMaxHp = getMaxHp();
    int tmpMaxMp = getMaxMp();
    transform(newNpcId);
    this._type = PetTypeTable.getInstance().get(newNpcId);
    setLevel(1);
    setMaxHp(tmpMaxHp / 2);
    setMaxMp(tmpMaxMp / 2);
    setCurrentHpDirect(getMaxHp());
    setCurrentMpDirect(getMaxMp());
    setExp(0L);
    setExpPercent(0);
    getInventory().consumeItem(evolvItem, 1L);
    L1Object obj = World.get().findObject(pet.get_objid());
    if (obj != null && obj instanceof L1NpcInstance) {
      L1PetInstance new_pet = (L1PetInstance)obj;
      L1Inventory new_petInventory = new_pet.getInventory();
      List<L1ItemInstance> itemList = getInventory().getItems();
      Iterator<L1ItemInstance> iterator = itemList.iterator();
      while (iterator.hasNext()) {
        Object itemObject = iterator.next();
        L1ItemInstance item = (L1ItemInstance)itemObject;
        if (item == null)
          continue; 
        if (item.isEquipped()) {
          item.setEquipped(false);
          L1PetItem petItem = PetItemTable.get().getTemplate(item.getItemId());
          if (petItem.isWeapom()) {
            setWeapon((L1ItemInstance)null);
            new_pet.usePetWeapon(this, item);
          } else {
            setArmor((L1ItemInstance)null);
            new_pet.usePetArmor(this, item);
          } 
        } 
        if (new_pet.getInventory().checkAddItem(item, item.getCount()) == 0) {
          getInventory().tradeItem(item, item.getCount(), new_petInventory);
          continue;
        } 
        L1GroundInventory groundinv = World.get().getInventory(getX(), getY(), 
            getMapId());
        getInventory().tradeItem(item, item.getCount(), (L1Inventory)groundinv);
      } 
      new_pet.broadcastPacketAll((ServerBasePacket)new S_SkillSound(new_pet.getId(), 2127));
    } 
    PetReading.get().deletePet(this._itemObjId);
    pet.set_itemobjid(new_itemobjid);
    pet.set_npcid(newNpcId);
    pet.set_name(getName());
    pet.set_level(getLevel());
    pet.set_hp(getMaxHp());
    pet.set_mp(getMaxMp());
    pet.set_exp((int)getExp());
    PetReading.get().storeNewPet(this, getId(), new_itemobjid);
    this._itemObjId = new_itemobjid;
  }
  
  public void liberate() {
    L1MonsterInstance monster = new L1MonsterInstance(getNpcTemplate());
    monster.setId(IdFactoryNpc.get().nextId());
    monster.setX(getX());
    monster.setY(getY());
    monster.setMap(getMapId());
    monster.setHeading(getHeading());
    monster.set_storeDroped(true);
    monster.setInventory(getInventory());
    setInventory((L1Inventory)null);
    monster.setLevel(getLevel());
    monster.setMaxHp(getMaxHp());
    monster.setCurrentHpDirect(getCurrentHp());
    monster.setMaxMp(getMaxMp());
    monster.setCurrentMpDirect(getCurrentMp());
    this._petMaster.getPetList().remove(Integer.valueOf(getId()));
    deleteMe();
    this._petMaster.getInventory().removeItem(this._itemObjId, 1L);
    PetReading.get().deletePet(this._itemObjId);
    World.get().storeObject((L1Object)monster);
    World.get().addVisibleObject((L1Object)monster);
    Iterator<L1PcInstance> iterator = World.get().getRecognizePlayer((L1Object)monster).iterator();
    while (iterator.hasNext()) {
      L1PcInstance pc = iterator.next();
      onPerceive(pc);
    } 
  }
  
  public void collect(boolean isDepositnpc) {
    L1PcInventory l1PcInventory = this._petMaster.getInventory();
    List<L1ItemInstance> items = this._inventory.getItems();
    Iterator<L1ItemInstance> iterator = items.iterator();
    while (iterator.hasNext()) {
      L1ItemInstance item = iterator.next();
      if (item.isEquipped()) {
        if (!isDepositnpc)
          continue; 
        int itemId = item.getItem().getItemId();
        L1PetItem petItem = PetItemTable.get().getTemplate(itemId);
        if (petItem != null) {
          setHitByWeapon(0);
          setDamageByWeapon(0);
          addStr(-petItem.getAddStr());
          addCon(-petItem.getAddCon());
          addDex(-petItem.getAddDex());
          addInt(-petItem.getAddInt());
          addWis(-petItem.getAddWis());
          addMaxHp(-petItem.getAddHp());
          addMaxMp(-petItem.getAddMp());
          addSp(-petItem.getAddSp());
          addMr(-petItem.getAddMr());
          setWeapon((L1ItemInstance)null);
          setArmor((L1ItemInstance)null);
          item.setEquipped(false);
        } 
      } 
      if (this._petMaster.getInventory().checkAddItem(item, item.getCount()) == 0) {
        this._inventory.tradeItem(item, item.getCount(), (L1Inventory)l1PcInventory);
        this._petMaster.sendPackets((ServerBasePacket)new S_ServerMessage(143, getName(), item.getLogName()));
        continue;
      } 
      item.set_showId(get_showId());
      L1GroundInventory l1GroundInventory = World.get().getInventory(getX(), getY(), getMapId());
      this._inventory.tradeItem(item, item.getCount(), (L1Inventory)l1GroundInventory);
    } 
    savePet();
  }
  
  public void dropItem() {
    L1GroundInventory l1GroundInventory = World.get().getInventory(getX(), getY(), getMapId());
    List<L1ItemInstance> items = this._inventory.getItems();
    Iterator<L1ItemInstance> iterator = items.iterator();
    while (iterator.hasNext()) {
      L1ItemInstance item = iterator.next();
      item.set_showId(get_showId());
      if (item.isEquipped()) {
        int itemId = item.getItem().getItemId();
        L1PetItem petItem = PetItemTable.get().getTemplate(itemId);
        if (petItem != null) {
          setHitByWeapon(0);
          setDamageByWeapon(0);
          addStr(-petItem.getAddStr());
          addCon(-petItem.getAddCon());
          addDex(-petItem.getAddDex());
          addInt(-petItem.getAddInt());
          addWis(-petItem.getAddWis());
          addMaxHp(-petItem.getAddHp());
          addMaxMp(-petItem.getAddMp());
          addSp(-petItem.getAddSp());
          addMr(-petItem.getAddMr());
          setWeapon((L1ItemInstance)null);
          setArmor((L1ItemInstance)null);
          item.setEquipped(false);
        } 
      } 
      this._inventory.tradeItem(item, item.getCount(), (L1Inventory)l1GroundInventory);
    } 
    savePet();
  }
  
  private void savePet() {
    try {
      L1Pet pet = PetReading.get().getTemplate(this._itemObjId);
      if (pet != null) {
        pet.set_exp((int)getExp());
        pet.set_level(getLevel());
        pet.set_hp(getMaxHp());
        pet.set_mp(getMaxMp());
        PetReading.get().storePet(pet);
      } 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  public void call() {
    if (this._type != null) {
      int id = this._type.getMessageId(L1PetType.getMessageNumber(getLevel()));
      if (id != 0)
        broadcastPacketAll((ServerBasePacket)new S_NpcChat(this, "$" + id)); 
    } 
    setCurrentPetStatus(7);
  }
  
  public void setTarget(L1Character target) {
    if (target != null && (
      this._currentPetStatus == 1 || this._currentPetStatus == 2 || this._currentPetStatus == 5)) {
      setHate(target, 0);
      if (!isAiRunning())
        startAI(); 
    } 
  }
  
  public void setMasterTarget(L1Character target) {
    if (target != null && (this._currentPetStatus == 1 || this._currentPetStatus == 5)) {
      setHate(target, 10);
      if (!isAiRunning())
        startAI(); 
    } 
  }
  
  public void setMasterSelectTarget(L1Character target) {
    if (target != null) {
      setHate(target, 0);
      if (!isAiRunning())
        startAI(); 
    } 
  }
  
  public void onPerceive(L1PcInstance perceivedFrom) {
    try {
      if (perceivedFrom.get_showId() != get_showId())
        return; 
      perceivedFrom.addKnownObject((L1Object)this);
      perceivedFrom.sendPackets((ServerBasePacket)new S_NPCPack_Pet(this, perceivedFrom));
      if (getMaster() != null && perceivedFrom.getId() == getMaster().getId())
        perceivedFrom.sendPackets((ServerBasePacket)new S_HPMeter(getId(), 100 * getCurrentHp() / getMaxHp())); 
      if (isDead())
        perceivedFrom.sendPackets((ServerBasePacket)new S_DoActionGFX(getId(), 8)); 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
  }
  
  public void onAction(L1PcInstance player) {
    if (player == null)
      return; 
    L1Character cha = getMaster();
    if (cha == null)
      return; 
    L1PcInstance master = (L1PcInstance)cha;
    if (master.isTeleport())
      return; 
    if (master.equals(player)) {
      L1AttackPc l1AttackPc1 = new L1AttackPc(player, this);
      l1AttackPc1.action();
      return;
    } 
    if (isSafetyZone() || player.isSafetyZone()) {
      L1AttackPc l1AttackPc1 = new L1AttackPc(player, this);
      l1AttackPc1.action();
      return;
    } 
    if (player.checkNonPvP(player, this))
      return; 
    L1AttackPc l1AttackPc = new L1AttackPc(player, this);
    if (l1AttackPc.calcHit())
      l1AttackPc.calcDamage(); 
    l1AttackPc.action();
    l1AttackPc.commit();
  }
  
  public void onTalkAction(L1PcInstance player) {
    if (isDead())
      return; 
    if (this._petMaster.equals(player)) {
      player.sendPackets((ServerBasePacket)new S_PetMenuPacket(this, getExpPercent()));
      savePet();
    } 
  }
  
  public void onFinalAction(L1PcInstance player, String action) {
    int status = Integer.parseInt(action);
    switch (status) {
      case 0:
        return;
      case 6:
        liberate();
        return;
    } 
    Object[] petList = this._petMaster.getPetList().values().toArray();
    Object[] array;
    int length = (array = petList).length;
    int i = 0;
    while (i < length) {
      Object petObject = array[i];
      if (petObject instanceof L1PetInstance) {
        L1PetInstance pet = (L1PetInstance)petObject;
        if (this._petMaster != null && this._petMaster.isGm()) {
          pet.setCurrentPetStatus(status);
        } else if (this._petMaster.getLevel() >= pet.getLevel()) {
          pet.setCurrentPetStatus(status);
        } else if (this._type != null) {
          int id = this._type.getDefyMessageId();
          if (id != 0)
            broadcastPacketAll((ServerBasePacket)new S_NpcChat(pet, "$" + id)); 
        } 
      } 
      i++;
    } 
  }
  
  public void onItemUse() {
    if (!isActived())
      useItem(1, 100); 
    if (getCurrentHp() * 100 / getMaxHp() < 60)
      useItem(0, 100); 
  }
  
  public void onGetItem(L1ItemInstance item) {
    if (getNpcTemplate().get_digestitem() > 0)
      setDigestItem(item); 
    Arrays.sort(L1NpcInstance.healPotions);
    Arrays.sort(L1NpcInstance.haestPotions);
    if (Arrays.binarySearch(L1NpcInstance.healPotions, item.getItem().getItemId()) >= 0) {
      if (getCurrentHp() != getMaxHp())
        useItem(0, 100); 
    } else if (Arrays.binarySearch(L1NpcInstance.haestPotions, item.getItem().getItemId()) >= 0) {
      useItem(1, 100);
    } 
  }
  
  public void setCurrentHp(int i) {
    int currentHp = Math.min(i, getMaxHp());
    if (getCurrentHp() == currentHp)
      return; 
    setCurrentHpDirect(currentHp);
    if (this._petMaster != null) {
      int hpRatio = 100 * currentHp / getMaxHp();
      L1PcInstance master = this._petMaster;
      master.sendPackets((ServerBasePacket)new S_HPMeter(getId(), hpRatio));
    } 
  }
  
  public void setCurrentMp(int i) {
    int currentMp = Math.min(i, getMaxMp());
    if (getCurrentMp() == currentMp)
      return; 
    setCurrentMpDirect(currentMp);
  }
  
  public void setCurrentPetStatus(int i) {
    this._currentPetStatus = i;
    set_tempModel();
    switch (this._currentPetStatus) {
      case 5:
        setHomeX(getX());
        setHomeY(getY());
        return;
      case 3:
        allTargetClear();
        return;
    } 
    if (!isAiRunning())
      startAI(); 
  }
  
  public int getCurrentPetStatus() {
    return this._currentPetStatus;
  }
  
  public int getItemObjId() {
    return this._itemObjId;
  }
  
  public void setExpPercent(int expPercent) {
    this._expPercent = expPercent;
  }
  
  public int getExpPercent() {
    return this._expPercent;
  }
  
  public void setWeapon(L1ItemInstance weapon) {
    this._weapon = weapon;
  }
  
  public L1ItemInstance getWeapon() {
    return this._weapon;
  }
  
  public void setArmor(L1ItemInstance armor) {
    this._armor = armor;
  }
  
  public L1ItemInstance getArmor() {
    return this._armor;
  }
  
  public void setHitByWeapon(int i) {
    this._hitByWeapon = i;
  }
  
  public int getHitByWeapon() {
    return this._hitByWeapon;
  }
  
  public void setDamageByWeapon(int i) {
    this._damageByWeapon = i;
  }
  
  public int getDamageByWeapon() {
    return this._damageByWeapon;
  }
  
  public L1PetType getPetType() {
    return this._type;
  }
  
  public void set_tempModel() {
    this._tempModel = this._currentPetStatus;
  }
  
  public void get_tempModel() {
    this._currentPetStatus = this._tempModel;
  }
  
  public void usePetWeapon(L1PetInstance pet, L1ItemInstance weapon) {
    if (pet.getWeapon() == null) {
      setPetWeapon(pet, weapon);
    } else if (pet.getWeapon().equals(weapon)) {
      removePetWeapon(pet, pet.getWeapon());
    } else {
      removePetWeapon(pet, pet.getWeapon());
      setPetWeapon(pet, weapon);
    } 
  }
  
  public void usePetArmor(L1PetInstance pet, L1ItemInstance armor) {
    if (pet.getArmor() == null) {
      setPetArmor(pet, armor);
    } else if (pet.getArmor().equals(armor)) {
      removePetArmor(pet, pet.getArmor());
    } else {
      removePetArmor(pet, pet.getArmor());
      setPetArmor(pet, armor);
    } 
  }
  
  private void setPetWeapon(L1PetInstance pet, L1ItemInstance weapon) {
    int itemId = weapon.getItem().getItemId();
    L1PetItem petItem = PetItemTable.get().getTemplate(itemId);
    if (petItem == null)
      return; 
    pet.setHitByWeapon(petItem.getHitModifier());
    pet.setDamageByWeapon(petItem.getDamageModifier());
    pet.addStr(petItem.getAddStr());
    pet.addCon(petItem.getAddCon());
    pet.addDex(petItem.getAddDex());
    pet.addInt(petItem.getAddInt());
    pet.addWis(petItem.getAddWis());
    pet.addMaxHp(petItem.getAddHp());
    pet.addMaxMp(petItem.getAddMp());
    pet.addSp(petItem.getAddSp());
    pet.addMr(petItem.getAddMr());
    pet.setWeapon(weapon);
    weapon.setEquipped(true);
  }
  
  private void removePetWeapon(L1PetInstance pet, L1ItemInstance weapon) {
    int itemId = weapon.getItem().getItemId();
    L1PetItem petItem = PetItemTable.get().getTemplate(itemId);
    if (petItem == null)
      return; 
    pet.setHitByWeapon(0);
    pet.setDamageByWeapon(0);
    pet.addStr(-petItem.getAddStr());
    pet.addCon(-petItem.getAddCon());
    pet.addDex(-petItem.getAddDex());
    pet.addInt(-petItem.getAddInt());
    pet.addWis(-petItem.getAddWis());
    pet.addMaxHp(-petItem.getAddHp());
    pet.addMaxMp(-petItem.getAddMp());
    pet.addSp(-petItem.getAddSp());
    pet.addMr(-petItem.getAddMr());
    pet.setWeapon((L1ItemInstance)null);
    weapon.setEquipped(false);
  }
  
  private void setPetArmor(L1PetInstance pet, L1ItemInstance armor) {
    int itemId = armor.getItem().getItemId();
    L1PetItem petItem = PetItemTable.get().getTemplate(itemId);
    if (petItem == null)
      return; 
    pet.addAc(petItem.getAddAc());
    pet.addStr(petItem.getAddStr());
    pet.addCon(petItem.getAddCon());
    pet.addDex(petItem.getAddDex());
    pet.addInt(petItem.getAddInt());
    pet.addWis(petItem.getAddWis());
    pet.addMaxHp(petItem.getAddHp());
    pet.addMaxMp(petItem.getAddMp());
    pet.addSp(petItem.getAddSp());
    pet.addMr(petItem.getAddMr());
    pet.setArmor(armor);
    armor.setEquipped(true);
  }
  
  private void removePetArmor(L1PetInstance pet, L1ItemInstance armor) {
    int itemId = armor.getItem().getItemId();
    L1PetItem petItem = PetItemTable.get().getTemplate(itemId);
    if (petItem == null)
      return; 
    pet.addAc(-petItem.getAddAc());
    pet.addStr(-petItem.getAddStr());
    pet.addCon(-petItem.getAddCon());
    pet.addDex(-petItem.getAddDex());
    pet.addInt(-petItem.getAddInt());
    pet.addWis(-petItem.getAddWis());
    pet.addMaxHp(-petItem.getAddHp());
    pet.addMaxMp(-petItem.getAddMp());
    pet.addSp(-petItem.getAddSp());
    pet.addMr(-petItem.getAddMr());
    pet.setArmor((L1ItemInstance)null);
    armor.setEquipped(false);
  }
  
  public void call_New() {
    if (this._type != null) {
      int id = this._type.getMessageId(L1PetType.getMessageNumber(getLevel()));
      if (id != 0)
        broadcastPacketAll((ServerBasePacket)new S_NpcChat(this, "$" + id)); 
    } 
    setCurrentPetStatus(1);
  }
}
